package com.fwmagic.spark.ml

import org.apache.spark.ml.linalg
import org.apache.spark.ml.linalg.Vectors

object VectorDemo {
  def main(args: Array[String]): Unit = {
    //构造一个密集型向量
    val dense: linalg.Vector = Vectors.dense(Array(18.0, 170, 175, 67, 1000))
    //[18.0,170.0,175.0,67.0,1000.0]
    println(dense)
    //(5,[0,1,2,3,4],[18.0,170.0,175.0,67.0,1000.0])
    //println(dense.toSparse)

    //构造一个稀疏型向量
    val sparse: linalg.Vector = Vectors.sparse(10, Array(2, 5), Array(33, 66))
    //(10,[2,5],[33.0,66.0])
    println(sparse)
    //转成密集型向量,其它位置补0
    //[0.0,0.0,33.0,0.0,0.0,66.0,0.0,0.0,0.0,0.0]
    println(sparse.toDense)

    println("===========================")

    //求向量1和向量2的距离的平方(勾股定理数字):(1-4)^2 + (1-5)^2 = 25
    //即：求向量之间欧式距离的工具方法
    val v1 = Vectors.dense(Array(1.0, 1))
    val v2 = Vectors.dense(Array(4.0, 5))

    val d: Double = Vectors.sqdist(v1, v2)
    //25
    println(d)
  }

}
